package com.ruoyi.web.thread;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.web.socket.WebSocketController;
import com.ruoyi.web.socket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

public class Examine implements Runnable {
    @Autowired
    private WebSocketServer webSocketServer;

    public Examine(WebSocketServer webSocketServer) {
        this.webSocketServer =webSocketServer;
    }

    @Override
    public void run() {
        Jedis jedis = new Jedis("43.143.104.112",6379);
        System.out.println("链接成功"+jedis.ping());
//        jedis.set("examine", String.valueOf(1));//测试注释
        while (true){
            String examine =  jedis.get("examine");
//            System.out.println(examine);
            if(examine == null || examine.equals("0")){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                continue;
            }
//            System.out.println(examine.split("")[0]);
//            System.out.println(examine.split("")[1]);
            if(examine.split("")[1].equals("1")){
                System.out.println("应该执行"+examine);
                //查询到有人发热上报
                //到对应的socker发消息
                jedis.set("examine", String.valueOf(0));
                WebSocketController.WebsocketResponse response = new WebSocketController.WebsocketResponse();
                response.setTitle("群发");
                response.setStatue(1);
                webSocketServer.sendMassMessage(JSONObject.toJSONString(response));
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
